<template>

   <MyHeader title="啥都有">
      <template #left>
         <svg class="icon" aria-hidden="true">
            <use xlink:href="#icon-31liebiao"></use>
         </svg>
      </template>
      <template #title>
         <van-search @focus="inSearch" shape="round" background="#c72418" placeholder="请输入搜索关键词" />
      </template>
      <template #right>
        <span v-if="isLogin">欢迎</span>
         <span v-else class="login"  @click="$router.push('/login')">登录</span>
      </template>
   </MyHeader>

   <banner></banner>
   <mynav></mynav>
   <mysd></mysd>
   <goodList></goodList>

</template>
<script setup lang="ts">
import banner from '@/components/home/banner.vue'
import mynav from '@/components/home/nav.vue'
import mysd from '@/components/home/sd.vue'
import { useRouter, type Router } from 'vue-router'
import goodList from '@/components/home/goodList.vue'
import { getUserInfoApi } from '@/api/users'
import { ref,onMounted } from 'vue'
const $router: Router = useRouter()
const inSearch=()=>{

   $router.push('/search')
}
const isLogin = ref<boolean>(false)
const avatar = ref<string>('')
const testToken = async () => {

  const token = window.localStorage.getItem('token')
  const userid = window.localStorage.getItem('user_id')

  
  if (!token || !userid) {
    isLogin.value = false
    console.log(isLogin.value);
    
    return
  }
  const res = await getUserInfoApi(userid)
  console.log(res);
  

  if (((res as unknown) as { code: string }).code === '10019') {
    isLogin.value = false
    return
  }console.log(res);
  isLogin.value = true
  avatar.value = res.data[0].avatar
  console.log(res)
}

onMounted(() => {
  testToken()
  
})

</script>

<style scoped lang="scss">
.icon {
   font-size: 24px;
   color: #fff;

}
.login{
   color:#fff;
}
.van-nav-bar__content{
   background-color: blue !important;
}
</style>